<?php
date_default_timezone_set('Asia/Shanghai');
require_once '../utils/common.php';
require_once '../Config.php';
$conn = mysql_connect ( host, username, password );
if (! $conn) {
	die ( "连接失败" . mysql_error () );
}
mysql_select_db ( db, $conn );
$type = $_REQUEST['type'];
if($type == 'add'){
	$username = $_REQUEST['username'];
	$password = $_REQUEST['password'];
	$phone = $_REQUEST['phone'];
	$deviceid = $_REQUEST['deviceid'];
	$time = date("Y-m-d H:i:s");
	$sql = "select count(*) from j_tab_user where username='".$username."' or phone='".$phone."'";
	$result = mysql_query($sql);
	if($row = mysql_fetch_array($result)){
		$count = $row[0];
	}
	if($count == 0){
		$sql = "set names utf8";
		mysql_query($sql);
		$sql = "insert into j_tab_user(username,password,phone,deviceid,time) values ('".$username."','".$password."','".$phone."','".$deviceid."','".$time."')";
		mysql_query($sql);
		mysql_close($conn);
		header("Location: UserManager.php");
	}else{
		echo "<script language=\"JavaScript\">\r\n";
		echo " alert(\"手机号或用户名已存在！请核对后重新添加\");\r\n";
		echo " history.back();\r\n";
		echo "</script>";
		exit;
	}
}else if($type == 'update'){
	$id = $_REQUEST['id'];
	$username = $_REQUEST['username'];
	$password = $_REQUEST['password'];
	$phone = $_REQUEST['phone'];
	$deviceid = $_REQUEST['deviceid'];
	$sql = "select count(*) from j_tab_user where (username='".$username."' or phone='".$phone."' or deviceid='".$deviceid."') and id != ".$id."";
	$result = mysql_query($sql);
	if($row = mysql_fetch_array($result)){
		$count = $row[0];
	}
	if($count == 0){
		$sql = "set names utf8";
		mysql_query($sql);
		$sql = "update j_tab_user set username='".$username."',password='".$password."',phone='".$phone."',deviceid='".$deviceid."' where id=".$id;
		mysql_query($sql);
		mysql_close($conn);
		header("Location: UserManager.php");
	}else{
		echo "<script language=\"JavaScript\">\r\n";
		echo " alert(\"手机号、用户名或设备号已存在！请核对后再修改\");\r\n";
		echo " history.back();\r\n";
		echo "</script>";
		exit;
	}
}else if($type == 'del'){
	$id = $_REQUEST['id'];
	$sql = "delete from j_tab_user where id=".$id;
	mysql_query($sql);
	mysql_close($conn);
	echo "<script language=\"JavaScript\">\r\n";
	echo " alert(\"删除成功！\");\r\n";
	echo " history.back();\r\n";
	echo "</script>";
	exit;
}



?>